<template>
  <el-dialog title="角色编辑" width="500px" v-model="dialogFormVisible">
    <el-form :model="form" size="small">
      <el-form-item label="角色名称" :label-width="formLabelWidth">
        <el-input v-model="form.role_name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="权限" :label-width="formLabelWidth">
        <el-tree
          :data="data"
          show-checkbox
          node-key="id"
          :default-expand-all="true"
          :default-expanded-keys="[]"
          :default-checked-keys="form.permission_ids"
          :props="defaultProps"
        >
        </el-tree>
      </el-form-item>
      <el-form-item label="描述" :label-width="formLabelWidth">
        <el-input
          type="textarea"
          v-model="form.role_describe"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="small" @click="cancelFunc()">取 消</el-button>
        <el-button size="small" type="primary" @click="confirmFunc()"
          >确 定</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      //是否显示
      dialogFormVisible: false,
      //表单数据
      form: {
        //角色名称
        role_name: "",
        //权限
        permission_ids:[2,3],
        //描述
        role_describe: "",
      },
      //左边标签宽度
      formLabelWidth: "120px",
      //角色数据
      data: [
        {
          id: 1,
          label: "一级菜单权限 1",
          children: [
            {
              id: 4,
              label: "二级菜单权限 1-1",
              children: [
                {
                  id: 9,
                  label: "三级菜单权限 1-1-1",
                },
                {
                  id: 10,
                  label: "三级菜单权限 1-1-2",
                },
              ],
            },
          ],
        },
        {
          id: 2,
          label: "一级菜单权限 2",
          children: [
            {
              id: 5,
              label: "二级菜单权限 2-1",
            },
            {
              id: 6,
              label: "二级菜单权限 2-2",
            },
          ],
        },
        {
          id: 3,
          label: "一级菜单权限 3",
          children: [
            {
              id: 7,
              label: "二级菜单权限 3-1",
            },
            {
              id: 8,
              label: "二级菜单权限 3-2",
            },
          ],
        },
      ],
      //属性值名称改写
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  props: {
    visible: Boolean,
  },
  watch: {
    //监听是否显示
    visible(newVal, oldVal) {
      if (newVal != oldVal) {
        this.dialogFormVisible = newVal;
      }
    },
  },
  created() {},
  methods: {
    /**
     * 确认
     */
    confirmFunc() {
      this.$message({
        type: "success",
        message: "操作成功!",
      });
      this.cancelFunc();
    },

    /**
     * 取消
     */
    cancelFunc() {
      this.$emit("close", false);
    },
  },
};
</script>

<style lang="scss">
</style>